package com.firebird.app.kotlin.stock.ui.allocation_plan

import android.R
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.widget.*
import butterknife.BindView
import butterknife.OnCheckedChanged
import butterknife.OnClick
import butterknife.OnTextChanged
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.MoneyRuleBean
import com.firebird.app.kotlin.stock.data.MultipleRuleBean
import com.firebird.app.kotlin.stock.ui.allocation_manage.Adp
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils.restLoginDialog
import com.firebird.app.kotlin.stock.utils.ToastUtil.showToast
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.data.EventMessage
import com.firebird.app.kotlin.stock.net.APIService
import com.firebird.app.kotlin.stock.ui.allocation_plan.contract.AllocationPlanContract
import com.firebird.app.kotlin.stock.ui.allocation_plan.presenter.AllocationPlanPresenter
import com.firebird.app.kotlin.stock.ui.free_interset.FreeintersetActivity
import com.firebird.app.kotlin.stock.ui.nullpage.NullPersonActivity
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil
import org.greenrobot.eventbus.EventBus


/**
 * 配置方案
 */
class AllocationPlanActivity : BaseMvpActivity<AllocationPlanPresenter?>(), AllocationPlanContract.View {
    @BindView(R.id.back)
    var back: LinearLayout? = null
    @BindView(R.id.et_money)
    var etMoney: EditText? = null
    @BindView(R.id.recycler)
    var recycler: RecyclerView? = null
    @BindView(R.id.bt_next)
    var btNext: TextView? = null
    @BindView(R.id.tv_title)
    var tvTitle: TextView? = null
    @BindView(R.id.rb_immediately)
    var rbImmediately: RadioButton? = null
    @BindView(R.id.rb_nextmon)
    var rbNextmon: RadioButton? = null
    private var list: List<MultipleRuleBean.RespDataBean?>? = null
    private var adp: Adp? = null
    private var bundle: Bundle? = null
    private var strIndex = 0
    private var bei = 0
    private var bpid = 0
    private var tiemType = 1
    private var pos = -1
    private var strMoney = "0"
    private var strlilv: String? = null
    override val layoutId: Int
        get() = R.layout.activity_allocation_plan

    override fun initView() {
        bundle = intent.extras
        strIndex = bundle.getInt("mIndex")
        setTitle()
        mPresenter = AllocationPlanPresenter()
        mPresenter.attachView(this)
        //获取金额规则
        mPresenter.getMoneyRuleData(strIndex)
        //获取倍数规则
        mPresenter.getMultipleRuleData(strIndex)
    }

    override fun showLoading() {}
    override fun hideLoading() {}
    override fun onError(throwable: Throwable?) {}
    fun onSuccess(bean: MoneyRuleBean) {
        if (bean.respCode == Config.SUCCESS) { //            备注：最少10 元，最多 2000元，且为 100 的倍数。
            val minAmt = bean.respData.minAmt.toDouble()
            val maxAmt = bean.respData.maxAmt.toDouble()
            etMoney!!.hint = minAmt.toString() + "元~" + maxAmt + "元(且为 100 的倍)。"
        } else if (bean.respCode == Config.TOKENOUT) {
            restLoginDialog(this)
        } else if (bean.respCode == Config.NONAME) {
            val message = EventMessage(1, "Main")
            EventBus.getDefault().postSticky(message)
            toClass1(this, NullPersonActivity::class.java)
        } else {
            showToast(this, bean.respDesc)
        }
    }

    @OnTextChanged(R.id.et_money)
    fun onTextChanged(text: CharSequence?) { //保证金
        strMoney = etMoney!!.text.toString().trim { it <= ' ' }
        if (TextUtils.isEmpty(strMoney)) {
            strMoney = "0"
        }
        adp.setMoney(strMoney)
    }

    fun onSuccess(bean: MultipleRuleBean) {
        if (bean.respCode == Config.SUCCESS) {
            list = bean.respData
            recycler.setLayoutManager(GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false))
            recycler.setNestedScrollingEnabled(false)
            adp = Adp(R.layout.item_allocation, list, strMoney)
            recycler.setAdapter(adp)
            adp!!.openLoadAnimation()
            adp!!.setOnItemClickListener { adapter, view, position ->
                pos = position
                adp.setSelectItem(position)
                adp.notifyDataSetChanged()
                //利率
                strlilv = list!![position].singleRate + ""
                //倍数
                bei = list!![position].times
                bpid = list!![position].bpId
                if (pos == -1) { //没有选中
                    btNext!!.alpha = 0.5f
                    btNext!!.isEnabled = false
                } else { //选中
                    btNext!!.alpha = 1f
                    btNext!!.isEnabled = true
                }
            }
        } else {
            showToast(this, bean.respDesc)
        }
    }

    @OnCheckedChanged(R.id.rb_immediately, R.id.rb_nextmon)
    fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
        when (buttonView.id) {
            R.id.rb_immediately -> if (isChecked) { //立即生效
                tiemType = 1
            }
            R.id.rb_nextmon -> if (isChecked) { //下个交易日
                tiemType = 2
            }
        }
    }

    @OnClick(R.id.back, R.id.bt_next)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.back -> finish()
            R.id.bt_next ->  //配资基金
                if (strMoney === "0") {
                    showToast(this, "请输入保证金")
                } else {
                    val peizi = strMoney.toDouble() * bei
                    bundle = Bundle()
                    bundle!!.putInt("mIndex", strIndex)
                    bundle!!.putString("lilv", strlilv)
                    bundle!!.putString("peizi", peizi.toString() + "")
                    bundle!!.putString("baozheng", strMoney)
                    bundle!!.putInt("timetype", tiemType)
                    bundle!!.putInt("bpid", bpid)
                    bundle!!.putInt("bei", bei)
                    toClass(this, FreeintersetActivity::class.java, bundle)
                }
        }
    }

    private fun setTitle() {
        when (strIndex) {
            Config.FREE -> tvTitle!!.text = "免息"
            Config.DAY -> tvTitle!!.text = "按日"
            Config.MON -> tvTitle!!.text = "按月"
        }
    }

    private fun text() {}
}